package timing.ukulele.flow.controller;

import timing.ukulele.flow.persistent.Flow;
import timing.ukulele.flow.service.FlowService;
import timing.ukulele.flow.service.ModelService;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.ResultCode;
import timing.ukulele.common.item.ItemData;
import timing.ukulele.flow.dictionary.FlowStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/flow")
@Api(tags = "流程接口")
public class FlowController {

    @Autowired
    private FlowService flowService;
    @Autowired
    private ModelService modelService;

    @GetMapping("/page")
    @ApiOperation("分页获取流程列表")
    public ResponseResult page(
            @ApiParam(value = "查询名称") @RequestParam(required = false) String name,
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
            @ApiParam(value = "模型flowKey") @RequestParam(required = false) String modelId,
            @ApiParam(value = "业务系统名称") @RequestParam(required = false) String systemName,
            @ApiParam(value = "业务系统id") @RequestParam(required = false) Long systemId,
            @ApiParam(value = "系统权限ID") @RequestHeader(Constant.SYSTEM) String systemIds) {
        // String systemIds = "1, 1326390830101028866, 1326391202156765185, 1326391346805727234, 1326391436677079042, 1326391548857933826, 1326391642428661761,"
        //     + "1326391804022611969, 1326391891947806721, 1326398858841829378, 1326807910528876546,1328902971995566081,1330761896646705153,1331786725621538817,1331798987254050817,"
        //     + "1331864533060300802,1341980290758352898,1422488072509575170";
        HashMap<String, Object> map = new HashMap<>();
        if (name != null) {
            map.put("name", name);
        }
        if (modelId != null) {
            map.put("modelId", modelId);
        }
        if (systemName != null) {
            map.put("systemName", systemName);
        }
        if (systemId != null) {
            map.put("systemId", systemId);
        }
        return ResponseResult.success(flowService.getPage(systemIds, pageNum, pageSize, map));
    }

    @GetMapping("/systemId/page")
    @ApiOperation("根据系统id获取流程列表")
    public ResponseResult pageBySystemId(
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
            @ApiParam(value = "使用状态", example = "1") @RequestParam(required = false) Integer status,
            @ApiParam(value = "使用状态", example = "1") @RequestParam(required = false) Integer active,
            @ApiParam(value = "系统权限ID", example = "1") @RequestParam Long systemId) {
        return ResponseResult.success(flowService.getPageBySystemId(systemId, pageNum, pageSize, status, active));
    }

    @PostMapping("/create")
    @ApiOperation("保存流程信息")
    @ApiImplicitParam(name = "flow", value = "流程对象", required = true, dataType = "Flow")
    public ResponseResult create(
            @ApiParam(value = "流程", required = true) @RequestBody Flow flow) {
        if (flow.getStatus() == null)
            flow.setStatus(FlowStatusEnum.INIT);
        flowService.save(flow);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改流程信息")
    @ApiImplicitParam(name = "flow", value = "流程对象", required = true, dataType = "Flow")
    public ResponseResult update(
            @ApiParam(value = "流程", required = true) @RequestBody Flow flow) {
        if (flow.getStatus() != null)
            flow.setStatus(null);
        flowService.updateById(flow);
        return ResponseResult.success();
    }

    @DeleteMapping
    @ApiOperation("删除流程信息")
    public ResponseResult<String> remove(@RequestParam("flowId") Long flowId) {
        ResponseResult<List<ItemData>> items = flowService.getFlowItems(flowId);
        if (items.getCode() != ResultCode.SUCCESS.getCode()) {
            return ResponseResult.error(items.getMsg());
        }
        return modelService.removeModel(flowId);
    }

}
